<?php

namespace App\Http\Controllers\Back;

use App\Model\Member;
use App\Model\MemberBooks;
use App\Util\Result;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Log;
use App\Util\TimeHelper;

class MemberController extends Controller
{

    private function leftBar() {
        return [
            'title' => '图书管理员管理',
            'list'  => [
                [
                    'title' => '图书管理员列表',
                    'type' => 'member_list',
                    'url' => url('back/member'),
                ],
            ]

        ];
    }

    public function __construct(Request $request){

        $this->request = $request;
        $this->limit = config('config.pageLimit');
        $leftBarArr = $this->leftBar();
        view()->share(['title' => $leftBarArr['title'],'list'=>$leftBarArr['list'],'limit'=>$this->limit]);
    }

    /**
     * Display a listing of the resource.
     * 用户帐号列表
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $res = Member::getList($this->limit)->getData();
        return view('back/member/index')
            ->with('res',$res['list'])
            ->with('count',$res['count'])
            ->with('titleType','member_list');
    }

    public function create()
    {
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
            $sel = DB::table('readers')->where('uid',$input['uid']);
            if ($sel->count()) {
                $res2 = $sel->update(['is_main'=>1,'updated_at'=>time()]);
                if ($res2) {
                    return Result::success()->setType(0)->getResult();
                } else {
                    return Result::failure()->settype(1)->getResult();
                }
            } else {
                return Result::failure()->settype(2)->setMsg('该用户不存在！')->getResult();
            }
        }else {
            return view('back/member/create');
        }
    }

    public function deleteMember($id) {
        $sel = DB::table('readers')
            ->where('id',$id);
        if (empty($sel->count())) {
            return Result::failure()->getResult();
        }
        $res = $sel->update(['is_main'=>null]);
        if ($res) {
            return Result::success()->getResult();
        } else {
            return Result::failure()->getResult();
        }
    }

    /**
     * 用户借还书
     * czs
     */
    public function memberBook($id) {
        $res = MemberBooks::getList($id,$this->limit)->getData();
        $this->request->flash();
        return view('back/member/memberBook')
            ->with('res',$res)
            ->with('id',$id);
    }

    /**
     * Show the form for editing the specified resource.
     * 用户编辑保存
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
            $input['birthday'] = strtotime($input['birthday']);
            $res = Member::updateData($input, $id);
            return $res->getResult();
        } else {
            $one = Member::getOne($id)->getData();
            return view('back/member/edit')
                ->with('one', $one);
        }
    }

    /**
     * @param $id
     * 禁用、启用
     */
    public function able($id,$able)
    {
        $res = Member::able($id,$able);
        return $res->getResult();
    }
}
